package net.xdlass.exception;

import lombok.extern.slf4j.Slf4j;
import net.xdlass.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.annotation.Resource;

@Slf4j
@RestControllerAdvice
public class BizExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public JsonData handleBizException(Exception e) {
        if (e instanceof BizException){
            BizException bizException = (BizException) e;
            log.error("[业务异常]{}",e);
            return JsonData.buildCodeAndMsg(bizException.getCode(), bizException.getMessage());

        }else {
            log.error("[系统异常]{}",e);
            return JsonData.buildError("系统异常");
        }
    }
}
